import pandas as pd
import numpy as np
import random
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
import numpy as np

df = pd.read_csv(r'E:\house.csv', encoding='gbk')

print(f'样本量共有 {df.shape[0]} 个')

print(df.duplicated().sum())
# 3. 判断是否有缺失值
print(df.isnull().sum())
# 4. 查看数据类型
print(df.dtypes)

print(df['朝向'].unique())
print(df['楼层'].unique())
print(df['装修'].unique())
print(df['产权性质'].unique())
print(df['住宅类别'].unique())
print(df['建筑结构'].unique())
print(df['建筑类别'].unique())
print(df['区域'].unique())
print(df['建筑年代'].unique())

df.replace('暂无',np.nan,inplace=True)
df['建筑面积'] = df['建筑面积'].map(lambda x: x.replace('平米','')).astype('float')
df['单价'] = df['单价'].map(lambda x: x.replace('元/平米','')).astype('float')
def process_year(year):
    if year is not None:
        year = str(year)[:4]
    return year
df['建筑年代'] = df['建筑年代'].map(process_year)
floor = {'低楼层': '低','中楼层': '中','高楼层': '高','低层': '低','中层': '中','高层': '高'}
df['楼层'] = df['楼层'].map(floor)
def process_area(area):
    if area != '新区':
        area = area.replace('区','').replace('县','')
    return area
df['区域'] = df['区域'].map(process_area)
df.replace('nan',np.nan,inplace=True)

df.drop_duplicates(inplace=True)
df.reset_index(drop=True, inplace=True)

df.info()

df.dropna(subset=['户型','朝向','楼层'], inplace=True)
df.loc[(df['楼层'] == '高') & (df['电梯'].isnull()),'电梯'] = '有 '
df.loc[(df['楼层'] == '低') & (df['电梯'].isnull()),'电梯'] = '无 '
df.loc[(df['楼层'] == '中') & (df['电梯'].isnull()),'电梯'] = random.choice(['有 ','无 '])
df.reset_index(drop=True, inplace=True)

# 箱线图分析
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig,ax = plt.subplots(1,2,figsize=(16,6))
df.boxplot(column=['建筑面积'], flierprops={'markeredgecolor':'red', 'markersize':4}, ax=ax[0])
df.boxplot(column=['总价'], flierprops={'markeredgecolor':'red', 'markersize':4}, ax=ax[1])

df.describe()

df.drop(index = df[df['总价'] > 200].index, inplace=True)
